import { _decorator, Component, instantiate, Node, ParticleSystem2D, Prefab, v2, Vec2 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('heartRoot')
export class heartRoot extends Component {
    @property(Prefab) heart:Prefab;
    start()
    {
    }
    async create()
    {
        for(let i = -250;i<250;i += 15)
        {
            await new Promise(resolve => setTimeout(resolve, 50))
            const heart0 = instantiate(this.heart);
            const heart1 = instantiate(this.heart);
            heart0.setPosition(i,this.getTopY(i) + 10);
            heart1.setPosition(i,this.getDownY(i) + 10);
            heart0.getComponent(ParticleSystem2D).angle = this.getAngle(heart0.getPosition().x,heart0.getPosition().y)
            heart1.getComponent(ParticleSystem2D).angle = this.getAngle(heart1.getPosition().x,heart1.getPosition().y)
            this.node.addChild(heart0);
            this.node.addChild(heart1);
            
        }
    }

    getAngle(x:number,y:number)
    {
        const a1 = v2(1,0);
        const a2 = v2(x,y);
        const angle = a1.angle(a2) * 180 / Math.PI
        return y > 0 ? angle:-angle
    }
    getTopY(x:number)
    {
        return 120 * Math.sqrt(1 - (0.008 * Math.abs(x) - 1) ** 2);
    }
    getDownY(x:number)
    {
        return 120 * (Math.acos(1 - 0.008 * Math.abs(x)) - Math.PI);
    }

    update(deltaTime: number) {
        
    }
}


